package com.tsingteng.cosfun.ui.found.adapter;


import android.content.Context;
import android.text.TextPaint;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.bean.ActivityListBean;
import com.tsingteng.cosfun.bean.OpusBean;
import com.tsingteng.cosfun.ui.found.FoundVideoPlay;
import com.tsingteng.cosfun.utils.TimeUtils;

import java.text.ParseException;
import java.util.List;

/**
 *
 * 活动列表 item
 * author zht
 */
public class FoundActivityAdapter extends BaseQuickAdapter<ActivityListBean,BaseViewHolder> {

    private Context mContext;
    private FoundVideoPlay mFoundVideoPlay;
    public FoundActivityAdapter(Context context, FoundVideoPlay foundVideoPlay) {
        super(R.layout.cosfun_found_item_adapter);
        this.mContext = context;
        this.mFoundVideoPlay=foundVideoPlay;
    }

    @Override
    protected void convert(BaseViewHolder helper, ActivityListBean item) {
        int index;
        TextView textView = helper.getView(R.id.found_item_name_tv);
        if ("发布中".equals(item.getCurrentStatus())){
            TextPaint p=textView.getPaint();
            p.setFakeBoldText(true);
            textView.setTextColor(mContext.getResources().getColor(R.color.color_found));
        }else{
            helper.setTextColor(R.id.found_item_name_tv,mContext.getResources().getColor(R.color.white));
        }
      helper.setText(R.id.found_item_name_tv,"#"+item.getActivityTitle());
        boolean today = false;
        boolean yesterday = false;
        try {
            today = TimeUtils.IsToday(TimeUtils.formatDateTime(item.getPushTime()));
            yesterday = TimeUtils.IsYesterday(TimeUtils.formatDateTime(item.getPushTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (today){
            helper.setText(R.id.found_item_time_tv,mContext.getResources().getString(R.string.today));
        }else if (yesterday){
            helper.setText(R.id.found_item_time_tv,mContext.getResources().getString(R.string.yesterday));
        }else{
            helper.setText(R.id.found_item_time_tv, TimeUtils.formatDateTimeMD(item.getPushTime()));
        }
        if ("1".equals(item.getIsRead())){
            helper.getView(R.id.read_view).setVisibility(View.GONE);
        }else if ("2".equals(item.getIsRead())){
            helper.getView(R.id.read_view).setVisibility(View.VISIBLE);
        }
        TextView mFoundCountTv = helper.getView(R.id.found_count_tv);
      if (item.getPrizeActivity()){
          helper.getView(R.id.found_item_prize_iv).setVisibility(View.VISIBLE);
      }else {
          helper.getView(R.id.found_item_prize_iv).setVisibility(View.GONE);
      }
        ImageView imageView=helper.getView(R.id.found_cover_iv);
        Glide.with(mContext).load(item.getCoverImg()).into(imageView);
        LinearLayout mFoundItemLi=helper.getView(R.id.found_item_li);
        mFoundItemLi.removeAllViews();
        if (null != item.getVideoList()&& 0 != item.getVideoList().size()){
            if (item.getVideoList().size()>3){
                index = 3;
            }else{
                index = item.getVideoList().size();
            }
            if (item.getVideoList().size() == 3){
                mFoundCountTv.setVisibility(View.VISIBLE);
                mFoundCountTv.setText(item.getActiveCount()+mContext.getResources().getString(R.string.join));
            }else{
                mFoundCountTv.setVisibility(View.GONE);
            }
            for (int i=0;i<index;i++) {
                View view = LayoutInflater.from(mContext).inflate(R.layout.item_found_video, null);
                ImageView mFoundVideoIv = (ImageView) view.findViewById(R.id.found_video_iv);
                ImageView mItemPlayIv=(ImageView) view.findViewById(R.id.item_play_iv);
                TextView mItemNumTv=(TextView) view.findViewById(R.id.item_num_tv);
                mItemNumTv.setText(String.valueOf(i));
                mFoundVideoIv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onClicks(item.getVideoList(),mItemNumTv.getText().toString());
                    }
                });
                mItemPlayIv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onClicks(item.getVideoList(),mItemNumTv.getText().toString());
                    }
                });
                String mGifUrl = item.getVideoList().get(i).getVideoGifUrl();
                String mCverUrl =item.getVideoList().get(i).getVideoCverUrl();
                if (null != mGifUrl &&!TextUtils.isEmpty(mGifUrl)) {
                    RequestOptions options = new RequestOptions();
                    options.error(R.drawable.pic_in_pin_empty);
                    options.placeholder(R.drawable.pic_in_pin_empty);
                    Glide.with(mContext).applyDefaultRequestOptions(options).load(mGifUrl)
                            .into(mFoundVideoIv);
                }else {
                    if (null != mCverUrl && !TextUtils.isEmpty(mCverUrl)) {
                        RequestOptions options = new RequestOptions();
                        options.error(R.drawable.pic_in_pin_empty);
                        options.placeholder(R.drawable.pic_in_pin_empty);
                        Glide.with(mContext).applyDefaultRequestOptions(options).load(mCverUrl)
                                .into(mFoundVideoIv);
                    }
                }
                mFoundItemLi.addView(view);
            }
        }else {
            mFoundCountTv.setVisibility(View.GONE);
        }
    }
    void onClicks(List<OpusBean> list,String position){
        mFoundVideoPlay.canPlayListener(list,position);
    }
}
